import logging

import redis

import config
from .log import LogManager
from .singleton import singleton


@singleton
class RedisConnector:
	def __init__(self):
		self.logger = LogManager()
		try:
			self.conn = redis.StrictRedis(host = config.REDIS_SERVER, port = config.REDIS_PORT, db = config.REDIS_DB,
										  password = config.REDIS_PASSWORD, charset = 'utf-8', decode_responses = True)
			self.conn.ping()
			self.logger.log(self.__class__.__name__, "Connect to Redis Server !")
		except redis.exceptions.ConnectionError as e:
			self.conn = None
			self.logger.log(self.__class__.__name__, "Failed to connect Redis Server:{0}".format(e),
							level = logging.ERROR)

	def get_conn(self):
		return self.conn
